//
//  CountDownViewController.m
//  countdownDemo
//
//  Created by wanglei on 17/3/8.
//  Copyright © 2017年 wanglei. All rights reserved.
//

#import "CountDownViewController.h"
#import "Masonry.h"
#import "UIView+ArrangeSubview.h"
#import "CountDown.h"

@interface CountDownViewController ()
@property (strong, nonatomic)  UILabel *dayLabel;
@property (strong, nonatomic)  UILabel *hourLabel;
@property (strong, nonatomic)  UILabel *minuteLabel;
@property (strong, nonatomic)  UILabel *secondLabel;
@property (strong, nonatomic)  UIView *contentView;
@property (strong, nonatomic)  UIButton *timeBtn;
@property (strong, nonatomic)  CountDown *countDownForBtn;
@property (strong, nonatomic)  CountDown *countDownForLabel;

@end

@implementation CountDownViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self initUI];
    _countDownForLabel = [[CountDown alloc] init];
    _countDownForBtn = [[CountDown alloc] init];
    ///方法一倒计时测试
    long long startLongLong = 1467713971000;
    long long finishLongLong = 1467714322000;
    [self startLongLongStartStamp:startLongLong longlongFinishStamp:finishLongLong];
}


- (void)initUI{
    CGFloat label_width = 40;
    CGFloat label_height = 40;
    self.contentView = [UIView new];
    self.contentView.backgroundColor = [UIColor brownColor];
    [self.view addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(4*label_width, label_height));
        make.center.equalTo(self.view);
    }];
    
    self.dayLabel = [UILabel new];
    self.dayLabel.textAlignment = NSTextAlignmentCenter;
    self.dayLabel.backgroundColor = [UIColor greenColor];
    self.dayLabel.text = @"1";
    self.dayLabel.font = [UIFont systemFontOfSize:15];
    [self.contentView addSubview:self.dayLabel];
    
    self.hourLabel = [UILabel new];
    self.hourLabel.textAlignment = NSTextAlignmentCenter;
    self.hourLabel.font = [UIFont systemFontOfSize:15];
    self.hourLabel.backgroundColor = [UIColor yellowColor];
    self.hourLabel.text = @"3";
    [self.contentView addSubview:self.hourLabel];
    
    self.minuteLabel = [UILabel new];
    self.minuteLabel.textAlignment = NSTextAlignmentCenter;
    self.minuteLabel.font = [UIFont systemFontOfSize:15];
    self.minuteLabel.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:self.minuteLabel];
    
    self.secondLabel = [UILabel new];
    self.secondLabel.textAlignment = NSTextAlignmentCenter;
    self.secondLabel.font = [UIFont systemFontOfSize:15];
    self.secondLabel.backgroundColor = [UIColor orangeColor];
    [self.contentView addSubview:self.secondLabel];
    
    [self.dayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.contentView);
        make.width.mas_equalTo(label_width);
        make.centerY.equalTo(self.contentView.mas_centerY);
    }];
    
    [self.hourLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(self.dayLabel);
        make.centerY.equalTo(self.contentView.mas_centerY);
    }];
    [self.minuteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(self.dayLabel);
        make.centerY.equalTo(self.contentView.mas_centerY);
    }];
    [self.secondLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(self.dayLabel);
        make.centerY.equalTo(self.contentView.mas_centerY);
    }];

   [self.contentView arrangeSubviewWithSpacingHorizontally:@[self.dayLabel,self.hourLabel,self.minuteLabel,self.secondLabel]];
    
}

///此方法用两个时间戳做参数进行倒计时
-(void)startLongLongStartStamp:(long long)strtLL longlongFinishStamp:(long long)finishLL{
    __weak __typeof(self) weakSelf= self;
    [_countDownForLabel countDownWithStartTimeStamp:strtLL finishTimeStamp:finishLL completeBlock:^(NSInteger day, NSInteger hour, NSInteger minute, NSInteger second) {
        NSLog(@"666");
        [weakSelf refreshUIDay:day hour:hour minute:minute second:second];
    }];
}



-(void)refreshUIDay:(NSInteger)day hour:(NSInteger)hour minute:(NSInteger)minute second:(NSInteger)second{
    if (day==0) {
        self.dayLabel.text = @"0天";
    }else{
        self.dayLabel.text = [NSString stringWithFormat:@"%ld天",(long)day];
    }
    if (hour<10&&hour) {
        self.hourLabel.text = [NSString stringWithFormat:@"0%ld时",(long)hour];
    }else{
        self.hourLabel.text = [NSString stringWithFormat:@"%ld时",(long)hour];
    }
    if (minute<10) {
        self.minuteLabel.text = [NSString stringWithFormat:@"0%ld分",(long)minute];
    }else{
        self.minuteLabel.text = [NSString stringWithFormat:@"%ld分",(long)minute];
    }
    if (second<10) {
        self.secondLabel.text = [NSString stringWithFormat:@"0%ld秒",(long)second];
    }else{
        self.secondLabel.text = [NSString stringWithFormat:@"%ld秒",(long)second];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
